install.packages("ipumsr")
Error in install.packages : Updating loaded packages
library(ipumsr)
Warning: package ‘ipumsr’ was built under R version 4.3.3
set_ipums_api_key("59cba10d8a5da536fc06b59d10166c045d174963a84c0d0a1cd77825", save = TRUE,)
Existing .Renviron file copied to /Users/felipevaldez/.Renviron_backup for backup purposes.
Error in `set_ipums_envvar()`:
! IPUMS_API_KEY already exists in .Renviron. To overwrite it, set `overwrite = TRUE`.
Backtrace:
1. ipumsr::set_ipums_api_key(...)
2. ipumsr:::set_ipums_envvar(IPUMS_API_KEY = api_key, save = save, overwrite = overwrite)
nhgis_data <- get_metadata_nhgis("datasets")
nhgis_data
install.packages("dplyr")
Error in install.packages : Updating loaded packages
library(dplyr)
nhgis_data |>
filter(group == "2023 American Community Survey") |>
select(name, description)
ds_meta <- get_metadata_nhgis(dataset = "2019_2023_ACS5a")
ds_meta
$name
[1] "2019_2023_ACS5a"
$nhgis_id
[1] "ds267"
$group
[1] "2023 American Community Survey"
$description
[1] "5-Year Data [2019-2023, Block Groups & Larger Areas]"
$sequence
[1] 6202
$has_multiple_data_types
[1] TRUE
$data_tables
$geog_levels
$geographic_instances
$breakdowns
NA
ds_meta$data_tables
install.packages("stringr")
trying URL 'https://cran.r-project.org/bin/macosx/big-sur-arm64/contrib/4.3/stringr_1.5.1.tgz'
Content type 'application/x-gzip' length 312862 bytes (305 KB)
==================================================
downloaded 305 KB
The downloaded binary packages are in
/var/folders/9m/2jtnq8_d5hb5vykhx9m_0d080000gp/T//RtmplBPhpU/downloaded_packages
library(stringr)
ds_meta$data_tables |>
filter(str_detect(description, "Tenure"))
my_extract <- define_extract_nhgis(
description = "Homeownership by race, PA",
datasets = ds_spec(
"2019_2023_ACS5a",
data_tables = c("B25003B", "B25003H", "B25003I"),
geog_levels = "county"
),
shapefiles = "us_county_2023_tl2023", # explore available shapefiles in IPUMS website
)
my_extract <- submit_extract(my_extract)
Successfully submitted IPUMS NHGIS extract number 13
my_extract <- wait_for_extract(my_extract)
Checking extract status...
Waiting 10 seconds...
Checking extract status...
Waiting 20 seconds...
Checking extract status...
IPUMS NHGIS extract 13 is ready to download.
my_extract <- download_extract(my_extract)
|
| | 0%
|
|=========== | 8%
|
|====================== | 16%
|
|================================= | 24%
|
|=========================================== | 32%
|
|====================================================== | 40%
|
|================================================================= | 48%
|
|============================================================================ | 57%
|
|======================================================================================= | 65%
|
|================================================================================================== | 73%
|
|============================================================================================================= | 81%
|
|======================================================================================================================= | 89%
|
|================================================================================================================================== | 97%
|
|======================================================================================================================================| 100%
|
| | 0%
|
|= | 0%
|
|= | 1%
|
|== | 1%
|
|== | 2%
|
|=== | 2%
|
|=== | 3%
|
|==== | 3%
|
|===== | 3%
|
|===== | 4%
|
|====== | 4%
|
|====== | 5%
|
|======= | 5%
|
|======= | 6%
|
|======== | 6%
|
|========= | 6%
|
|========= | 7%
|
|========== | 7%
|
|========== | 8%
|
|=========== | 8%
|
|=========== | 9%
|
|============ | 9%
|
|============= | 9%
|
|============= | 10%
|
|============== | 10%
|
|============== | 11%
|
|=============== | 11%
|
|=============== | 12%
|
|================ | 12%
|
|================= | 12%
|
|================= | 13%
|
|================== | 13%
|
|================== | 14%
|
|=================== | 14%
|
|=================== | 15%
|
|==================== | 15%
|
|===================== | 15%
|
|===================== | 16%
|
|====================== | 16%
|
|====================== | 17%
|
|======================= | 17%
|
|======================= | 18%
|
|======================== | 18%
|
|========================= | 18%
|
|========================= | 19%
|
|========================== | 19%
|
|========================== | 20%
|
|=========================== | 20%
|
|=========================== | 21%
|
|============================ | 21%
|
|============================= | 21%
|
|============================= | 22%
|
|============================== | 22%
|
|============================== | 23%
|
|=============================== | 23%
|
|=============================== | 24%
|
|================================ | 24%
|
|================================= | 24%
|
|================================= | 25%
|
|================================== | 25%
|
|================================== | 26%
|
|=================================== | 26%
|
|==================================== | 26%
|
|==================================== | 27%
|
|===================================== | 27%
|
|===================================== | 28%
|
|====================================== | 28%
|
|====================================== | 29%
|
|======================================= | 29%
|
|======================================== | 29%
|
|======================================== | 30%
|
|========================================= | 30%
|
|========================================= | 31%
|
|========================================== | 31%
|
|========================================== | 32%
|
|=========================================== | 32%
|
|============================================ | 32%
|
|============================================ | 33%
|
|============================================= | 33%
|
|============================================= | 34%
|
|============================================== | 34%
|
|============================================== | 35%
|
|=============================================== | 35%
|
|================================================ | 35%
|
|================================================ | 36%
|
|================================================= | 36%
|
|================================================= | 37%
|
|================================================== | 37%
|
|================================================== | 38%
|
|=================================================== | 38%
|
|==================================================== | 38%
|
|==================================================== | 39%
|
|===================================================== | 39%
|
|===================================================== | 40%
|
|====================================================== | 40%
|
|====================================================== | 41%
|
|======================================================= | 41%
|
|======================================================== | 41%
|
|======================================================== | 42%
|
|========================================================= | 42%
|
|========================================================= | 43%
|
|========================================================== | 43%
|
|========================================================== | 44%
|
|=========================================================== | 44%
|
|============================================================ | 44%
|
|============================================================ | 45%
|
|============================================================= | 45%
|
|============================================================= | 46%
|
|============================================================== | 46%
|
|============================================================== | 47%
|
|=============================================================== | 47%
|
|================================================================ | 47%
|
|================================================================ | 48%
|
|================================================================= | 48%
|
|================================================================= | 49%
|
|================================================================== | 49%
|
|================================================================== | 50%
|
|=================================================================== | 50%
|
|==================================================================== | 50%
|
|==================================================================== | 51%
|
|===================================================================== | 51%
|
|===================================================================== | 52%
|
|====================================================================== | 52%
|
|====================================================================== | 53%
|
|======================================================================= | 53%
|
|======================================================================== | 53%
|
|======================================================================== | 54%
|
|========================================================================= | 54%
|
|========================================================================= | 55%
|
|========================================================================== | 55%
|
|========================================================================== | 56%
|
|=========================================================================== | 56%
|
|============================================================================ | 56%
|
|============================================================================ | 57%
|
|============================================================================= | 57%
|
|============================================================================= | 58%
|
|============================================================================== | 58%
|
|============================================================================== | 59%
|
|=============================================================================== | 59%
|
|================================================================================ | 59%
|
|================================================================================ | 60%
|
|================================================================================= | 60%
|
|================================================================================= | 61%
|
|================================================================================== | 61%
|
|================================================================================== | 62%
|
|=================================================================================== | 62%
|
|==================================================================================== | 62%
|
|==================================================================================== | 63%
|
|===================================================================================== | 63%
|
|===================================================================================== | 64%
|
|====================================================================================== | 64%
|
|====================================================================================== | 65%
|
|======================================================================================= | 65%
|
|======================================================================================== | 65%
|
|======================================================================================== | 66%
|
|========================================================================================= | 66%
|
|========================================================================================= | 67%
|
|========================================================================================== | 67%
|
|========================================================================================== | 68%
|
|=========================================================================================== | 68%
|
|============================================================================================ | 68%
|
|============================================================================================ | 69%
|
|============================================================================================= | 69%
|
|============================================================================================= | 70%
|
|============================================================================================== | 70%
|
|============================================================================================== | 71%
|
|=============================================================================================== | 71%
|
|================================================================================================ | 71%
|
|================================================================================================ | 72%
|
|================================================================================================= | 72%
|
|================================================================================================= | 73%
|
|================================================================================================== | 73%
|
|================================================================================================== | 74%
|
|=================================================================================================== | 74%
|
|==================================================================================================== | 74%
|
|==================================================================================================== | 75%
|
|===================================================================================================== | 75%
|
|===================================================================================================== | 76%
|
|====================================================================================================== | 76%
|
|======================================================================================================= | 76%
|
|======================================================================================================= | 77%
|
|======================================================================================================== | 77%
|
|======================================================================================================== | 78%
|
|========================================================================================================= | 78%
|
|========================================================================================================= | 79%
|
|========================================================================================================== | 79%
|
|=========================================================================================================== | 79%
|
|=========================================================================================================== | 80%
|
|============================================================================================================ | 80%
|
|============================================================================================================ | 81%
|
|============================================================================================================= | 81%
|
|============================================================================================================= | 82%
|
|============================================================================================================== | 82%
|
|=============================================================================================================== | 82%
|
|=============================================================================================================== | 83%
|
|================================================================================================================ | 83%
|
|================================================================================================================ | 84%
|
|================================================================================================================= | 84%
|
|================================================================================================================= | 85%
|
|================================================================================================================== | 85%
|
|=================================================================================================================== | 85%
|
|=================================================================================================================== | 86%
|
|==================================================================================================================== | 86%
|
|==================================================================================================================== | 87%
|
|===================================================================================================================== | 87%
|
|===================================================================================================================== | 88%
|
|====================================================================================================================== | 88%
|
|======================================================================================================================= | 88%
|
|======================================================================================================================= | 89%
|
|======================================================================================================================== | 89%
|
|======================================================================================================================== | 90%
|
|========================================================================================================================= | 90%
|
|========================================================================================================================= | 91%
|
|========================================================================================================================== | 91%
|
|=========================================================================================================================== | 91%
|
|=========================================================================================================================== | 92%
|
|============================================================================================================================ | 92%
|
|============================================================================================================================ | 93%
|
|============================================================================================================================= | 93%
|
|============================================================================================================================= | 94%
|
|============================================================================================================================== | 94%
|
|=============================================================================================================================== | 94%
|
|=============================================================================================================================== | 95%
|
|================================================================================================================================ | 95%
|
|================================================================================================================================ | 96%
|
|================================================================================================================================= | 96%
|
|================================================================================================================================= | 97%
|
|================================================================================================================================== | 97%
|
|=================================================================================================================================== | 97%
|
|=================================================================================================================================== | 98%
|
|==================================================================================================================================== | 98%
|
|==================================================================================================================================== | 99%
|
|===================================================================================================================================== | 99%
|
|===================================================================================================================================== | 100%
|
|======================================================================================================================================| 100%
Data file saved to /Users/felipevaldez/Documents/GitHub/IPUMSR_workshop25/nhgis0013_csv.zip
Shapefile saved to /Users/felipevaldez/Documents/GitHub/IPUMSR_workshop25/nhgis0013_shape.zip
nhgis_data <- read_nhgis("nhgis0013_csv.zip")
Use of data from NHGIS is subject to conditions including that users should cite the data appropriately. Use command `ipums_conditions()` for more details.
indexed 459.00B in 0s, 58.34MB/s
indexed 1.00TB in 0s, 265.26TB/s
Rows: 3222 Columns: 60── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (13): GISJOIN, YEAR, STUSAB, STATE, STATEA, COUNTY, COUNTYA, CNECTA, NECTADIV, GEO_ID, TL_GEO_ID, NAME_E, NAME_M
dbl (18): ASTBE001, ASTBE002, ASTBE003, ASTHE001, ASTHE002, ASTHE003, ASTIE001, ASTIE002, ASTIE003, ASTBM001, ASTBM002, ASTBM003, ASTHM001,...
lgl (29): REGIONA, DIVISIONA, COUSUBA, PLACEA, TRACTA, BLKGRPA, CONCITA, AIANHHA, RES_ONLYA, TRUSTA, AIHHTLI, AITSA, ANRCA, CBSAA, CSAA, ME...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
spatial_data <- read_ipums_sf("nhgis0013_shape.zip")
colnames(nhgis_data)
[1] "GISJOIN" "YEAR" "STUSAB" "REGIONA" "DIVISIONA" "STATE" "STATEA" "COUNTY" "COUNTYA" "COUSUBA" "PLACEA"
[12] "TRACTA" "BLKGRPA" "CONCITA" "AIANHHA" "RES_ONLYA" "TRUSTA" "AIHHTLI" "AITSA" "ANRCA" "CBSAA" "CSAA"
[23] "METDIVA" "CNECTA" "NECTADIV" "UAA" "CDCURRA" "SLDUA" "SLDLA" "ZCTA5A" "SUBMCDA" "SDELMA" "SDSECA"
[34] "SDUNIA" "PCI" "PUMAA" "GEO_ID" "BTTRA" "BTBGA" "TL_GEO_ID" "NAME_E" "ASTBE001" "ASTBE002" "ASTBE003"
[45] "ASTHE001" "ASTHE002" "ASTHE003" "ASTIE001" "ASTIE002" "ASTIE003" "NAME_M" "ASTBM001" "ASTBM002" "ASTBM003" "ASTHM001"
[56] "ASTHM002" "ASTHM003" "ASTIM001" "ASTIM002" "ASTIM003"
ipums_var_info(nhgis_data$ASTBE001)
nhgis_data <- nhgis_data |>
mutate(
ho_prop_white = ASTHE002 / ASTHE001,
ho_prop_black = ASTBE002 / ASTBE001,
ho_prop_hispanic = ASTIE002 / ASTIE001
)|>
mutate(
ho_ratio_white_black = ho_prop_white / ho_prop_black,
ho_ratio_white_hispanic = ho_prop_white / ho_prop_hispanic,
)|>
select(YEAR, STATEA, STATE, COUNTYA, COUNTY, GISJOIN, starts_with("ho_ratio")) |>
filter(STATEA==42)
for_mapping <- ipums_shape_inner_join(nhgis_data, spatial_data, by = "GISJOIN")
Some observations were lost in the join (3155 observations in the shape file).
See `join_failures(...)` for more details.
install.packages("ggplot2")
trying URL 'https://cran.r-project.org/bin/macosx/big-sur-arm64/contrib/4.3/ggplot2_3.5.1.tgz'
Content type 'application/x-gzip' length 4967644 bytes (4.7 MB)
==================================================
downloaded 4.7 MB
The downloaded binary packages are in
/var/folders/9m/2jtnq8_d5hb5vykhx9m_0d080000gp/T//RtmplBPhpU/downloaded_packages
library(ggplot2)
sf is a package to manage spatial formatsinstall.packages("sf")
library(sf)
ggplot(data = for_mapping, aes(fill = ho_ratio_white_hispanic)) +
geom_sf()
ggplot(data = for_mapping, aes(fill = ho_ratio_white_hispanic)) +
geom_sf() +
scale_fill_distiller(palette = "YlOrRd",
direction = 1) +
labs(title = "Homeownership ratio white/hispanic, PA, 2023",
caption = "Data source: 2022 5-year ACS, US Census Bureau",
fill = "Ratio") +
theme_void()
mapgl()install.packages("mapgl")
library(mapgl)
maplibre()
pa_map <- maplibre(bounds=for_mapping)
pa_map |>
add_fill_layer(
id = "homeowner",
source = for_mapping,
fill_color = interpolate(
column = "ho_ratio_white_hispanic",
values = c(1, 3),
stops = c("lightyellow", "darkorange"),
na_color = "lightgrey"
),
fill_opacity = 0.7
) |>
add_legend(
"Homeownership ratio white/hispanic, PA, 2023",
values = c(1, 3),
colors = c("lightyellow", "darkorange")
)